/**
 * 邀请记录
 */
import { apiGetSubjectInvitedRecord } from '@api/overview/subject'
import { apiGetPIInvitedRecord } from '@api/overview/member'

export default {
  data () {
    return {
      invitedRecords: [] // 邀请记录
    }
  },
  methods: {
    /**
     * 获取 受试者/研究者 邀请记录
     * @param {string} type subject-受试者 pi-研究者
     * result {*}
     */
    getInvitedRecord (type) {
      const params = this.getIRRequestParams(type)
      const request = {
        subject: apiGetSubjectInvitedRecord,
        pi: apiGetPIInvitedRecord
      }
      if (!request[type]) return
      return request[type](params).then(res => {
        this.invitedRecords = this.formatInvitedData(res)
      })
    },
    getIRRequestParams (type) {
      const getParamsStrategies = {
        subject: () => {
          const { id, proId } = this.pts
          const params = {
            proId,
            ptsId: id
          }
          return params
        },
        pi: () => {
          const pd = +localStorage.getItem('CURRENT_PROID')
          const params = {
            teamId: this.id,
            proId: pd
          }
          return params
        }
      }
      if (typeof getParamsStrategies[type] !== 'function') return {}
      return getParamsStrategies[type]()
    },
    /**
     * 邀请记录数据格式化
     * @param data
     * @return {*|*[]}
     */
    formatInvitedData (data) {
      if (!data || !Array.isArray(data)) return []
      return data.map(e => {
        const date = this.$options.filters.moment(e.createTime, 'YYYY-MM-DD')
        const dateTime = this.$options.filters.moment(e.createTime, 'HH:mm:ss')
        const invitedStatus = Object.values(this.$const.INVITED_STATUS).find(item => item.value === +e.status)?.desc || ''
        const description = [
          { label: this.$t('all.autotext239'), value: e.invitedName },
          { label: this.$t('all.autotext1503'), value: e.roleName },
          { label: this.$t('all.autotext460'), value: e.invitedPhone },
          {
            label: this.$t('all.autotext667'),
            value: invitedStatus,
            style: {
              color: 'var(--var-primary-color)'
            }
          }
        ]

        if (e.wechatName) {
          description.push(
            { label: this.$t('all.autotext1271'), value: e.wechatName }
          )
        }
        return {
          date,
          dateTime,
          title: e.operation,
          subtitle: `${this.$t('all.autotext840')}：${e.inviteAccount}`,
          description
        }
      })
    }
  }
}
